//
//  StatusView.m
//  TwitterPlane
//
//  Created by kazuyuki takahashi on 10/01/23.
//  Copyright 2010 by invisibledesigner.com. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "StatusView.h"
#import "StatusListView.h"


@implementation StatusView


- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        backgroundAlpha_ = 0.95;
    }
    return self;
}

- (void)updateTrackingAreas
{
    //boundsが変化無ければ処理を省く
    NSRect last = lastBounds_;
    NSRect currentBounds = [self bounds];
    lastBounds_ = currentBounds;
    
    if (NSEqualRects(last, currentBounds)) {
        return;
    }
    
    for (NSTrackingArea *a in [[[self trackingAreas] copy] autorelease]) {
        [self removeTrackingArea:a];
    }
    
    NSSize size = [self frame].size;
    NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect;
    NSTrackingArea *ta = [[[NSTrackingArea alloc] initWithRect:NSMakeRect(0, 0, size.width, size.height) 
                                                       options:options
                                                         owner:self
                                                      userInfo:nil] autorelease];
    [self addTrackingArea:ta];
}

- (void)drawRect:(NSRect)dirtyRect {
    const CGFloat kCorner = 12;
    
    NSSize size = [self frame].size;
    NSRect rect = NSMakeRect(0, kStatusListViewMargin, size.width + kCorner, size.height - kStatusListViewMargin);
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect
                                                         xRadius:kCorner
                                                         yRadius:kCorner];
    [[NSColor colorWithDeviceWhite:0.2 alpha:backgroundAlpha_] set];
    [path fill];
    
    [path setLineWidth:1.5];
    [[NSColor colorWithDeviceWhite:1.0 alpha:0.8] set];
    [path stroke];
    
    //隙間でもマウスホイールでスクロールできるようにするための極薄矩形を描画
    NSRect dammyrect = NSMakeRect(kCorner, 0, size.width, kStatusListViewMargin);
    [[NSColor colorWithDeviceWhite:1 alpha:0.1] set];
    NSRectFill(dammyrect);
    
    [super drawRect:dirtyRect];
}


- (id)animationForKey:(NSString *)key
{
    if ([key isEqualToString:@"frameOrigin"]) {
        CAAnimation *animation = [CABasicAnimation animation];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
        return animation;
    }
    return [super animationForKey:key];
}


@end
